package org.jdemo.orm.mybatis.support;

import org.jdemo.orm.mybatis.support.mybatis.MybatisContext;
import org.jdemo.util.CollectionUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Parameter
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 202014/4/15
 * @since V1.0
 */
public class Parameter {
    /**
     * 升序
     */
    public static final String DIR_ASC = "ASC";
    /**
     * 降序
     */
    public static final String DIR_DESC = "DESC";

    private Map<String, Object> query;
    private LinkedHashMap<String, String> sorts;
    private Integer start;
    private Integer limit;

    public static Parameter query(String key, Object val) {
        Parameter param = new Parameter();
        param.addQuery(key, val);
        return param;
    }

    public Parameter() {
    }

    public Parameter setQuery(Map<String, Object> query) {
        this.query = query;
        return this;
    }

    public Parameter addQuery(String key, Object val) {
        if (query == null) {
            query = new HashMap<String, Object>();
        }
        this.query.put(key, val);
        return this;
    }

    public Parameter setSorts(LinkedHashMap<String, String> sorts) {
        this.sorts = sorts;
        return this;
    }

    public Parameter addSort(String property, String direction) {
        if (this.sorts == null) {
            this.sorts = new LinkedHashMap<String, String>();
        }
        this.sorts.put(property, direction);
        return this;
    }

    public Parameter start(Integer start) {
        this.start = start;
        return this;
    }

    public Parameter limit(Integer limit) {
        this.limit = limit;
        return this;
    }

    public Map<String, Object> getQuery() {
        return query;
    }

    public LinkedHashMap<String, String> getSorts() {
        return sorts;
    }

    public Integer getStart() {
        return start;
    }

    public Integer getLimit() {
        return limit;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(MybatisContext.QUERY, query);
        map.put(MybatisContext.SORTS, sorts);
        map.put(MybatisContext.START, start);
        map.put(MybatisContext.LIMIT, limit);
        return map;
    }

    public Map<String, Object> toMap(String tableName) {
        Map<String, Object> map = this.toMap();
        if (tableName != null && tableName.length() > 0) {
            map.put(MybatisContext.TABLE_NAME, tableName);
        }
        return map;
    }

    public Map<String, Object> toMap(String tableName, List<String> tableColumns) {
        Map<String, Object> map = this.toMap(tableName);
        if (tableColumns != null && tableColumns.size() > 0) {
            map.put(MybatisContext.TABLE_COLUMNS, CollectionUtil.join(tableColumns, ","));
        }
        return map;
    }

    public Map<String, Object> toMap(String tableName, String fxName, String fxColumn) {
        Map<String, Object> map = this.toMap(tableName);
        if (fxName != null && fxName.length() > 0) {
            map.put(MybatisContext.FX_NAME, fxName);
        }
        if (fxColumn != null && fxColumn.length() > 0) {
            map.put(MybatisContext.FX_COLUMN, fxColumn);
        }
        return map;
    }
}
